package test;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2023-01-10
 * Time: 18:51
 */
class Singleton1{
    //懒汉不急着创建实例
    //volatile 防止指令重排序
    private volatile static Singleton1 instance = null;
    //构造函数 防止类外修改
    private Singleton1(){}
    //创建一个类外拿到的方法
    public static Singleton1 getInstance(){
        //双重if
        if (instance == null){
            //加锁
            synchronized (Singleton1.class){
                if (instance == null){
                    instance = new Singleton1();
                }
            }
        }
        return instance;
    }
}


//单例模式 - 懒汉模式
public class Demo2 {
    public static void main(String[] args) {
        Singleton1 instance = Singleton1.getInstance();
    }
}
